package com.managers
{
	import com.events.SSJEvent;
	import com.events.SSJEventInit;
	
	import flash.display.Bitmap;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import mx.managers.CursorManager;
	import mx.managers.CursorManagerPriority;
	
	import org.osmf.events.DisplayObjectEvent;
	
	import spark.components.Window;

	/**
	 * 鼠标管理器 
	 * @author Administrator
	 * 
	 */
	public class MouseManager
	{
		private static var _rect : Rectangle;			//当鼠标带有数据时检测可执行区域
		private static var _root : Stage;				
		
		[Embed(source="assets/uisource/notplaced.png")]
		private static var notPlacedClass : Class;
		private static var notPlacedBitmap : Bitmap = new notPlacedClass();
		
		[Embed(source="assets/uisource/canplaced.png")]
		private static var canPlacedClass : Class;
		private static var canPlacedBitmap : Bitmap = new canPlacedClass();
		
		private static var boo : Boolean = false;
		private static var activeboo : Boolean = false;
		
		private static var _target : *;
		private static var _type : String;
		
		public function MouseManager()
		{
		}
		
		/**
		 * 初始化鼠标管理器 
		 * @param rect
		 * @param root
		 * 
		 */		
		public static function Init(rect : Rectangle, root : Stage) : void
		{
			_rect = rect;
			_root = root;
			_root.addEventListener(MouseEvent.MOUSE_MOVE, onRootMouseMove);
			_root.addEventListener(MouseEvent.MOUSE_UP, onRootMouseUp);
		}
		
		/**
		 * 设置鼠标选择对象 
		 * @param target
		 * @param type		
		 */		
		public static function Hand(target : *,type:String) : void
		{
			_target = target;
			_type = type;
			activeboo = true;
			notPlaced();
		}
		
		private static function onRootMouseMove(event : MouseEvent) : void
		{
			if(!activeboo)return;
			boo = _rect.contains(event.stageX,event.stageY);
			if(boo){
				canPlaced();
			}else{
				notPlaced();
			}
		}
		
		private static function onRootMouseUp(event : MouseEvent) : void
		{
			CursorManager.removeAllCursors();
			if(!boo)
			{
				removeHand();
			}else{
				_root.dispatchEvent(new SSJEvent(SSJEventInit.RECIVE_HAND,[_target,_type]));
			}
			activeboo = false;
		}
		
		/**
		 * 设置当前检测区域不可执行 
		 * 
		 */		
		public static function notPlaced() : void
		{
			CursorManager.removeAllCursors();
			CursorManager.setCursor(notPlacedClass,CursorManagerPriority.HIGH);
		}
		
		/**
		 * 设置当前检测区域可执行 
		 * 
		 */		
		public static function canPlaced() : void
		{
			CursorManager.removeAllCursors();
			CursorManager.setCursor(canPlacedClass,CursorManagerPriority.HIGH);
		}
		
		/**
		 * 更新鼠标可检测区域 
		 * @param rect
		 * 
		 */		
		public static function set updatarect(rect : Rectangle) : void
		{
			_rect = rect;
		}
		
		/**
		 * 获取鼠标数据对象 
		 * @return 
		 * 
		 */		
		public static function get handData() : *
		{
			return _target;
		}
		
		/**
		 * 删除鼠标数据对象 
		 * 
		 */		
		public static function removeHand() : void
		{
			_target = null;
			_type = null;
			boo = false;
			activeboo = false;
		}
		
		/**
		 * 销毁鼠标管理器 
		 * 
		 */		
		public static function destroy() : void
		{
			removeHand();
			_root.removeEventListener(MouseEvent.MOUSE_MOVE, onRootMouseMove);
			_root.removeEventListener(MouseEvent.MOUSE_UP, onRootMouseUp);
		}
	}
}